<?php
/**
 * Created by PhpStorm.
 * User: qinkai
 * Date: 2019/5/9
 * Time: 15:35
 * Email: qinkai@ucex.pro
 */

namespace app\traits;


trait HttpResponse
{


    public function fail($message, $errorCode)
    {
        return json([
            'message' => $message,
            'error' => $errorCode,
            'data' => null
        ], 447)->options([
            'json_encode_param' => JSON_UNESCAPED_UNICODE
        ])->header([
            'Content-Type' => 'json'
        ]);
    }

    public function renderJson($data = [], $message = 'success')
    {
        return json([
            'message' => $message,
            'error' => 0,
            'data' => $data
        ], 200)->options([
            'json_encode_param' => JSON_UNESCAPED_UNICODE
        ])->header([
            'Content-Type' => 'json'
        ]);
    }

    public function exitJson($message, $errorCode)
    {
        header('HTTP/1.1 447 unauthorized');
        exit(json_encode([
            'message' => $message,
            'error' => $errorCode,
            'data' => null
        ]));
    }


}
